package com.yyfax.common.form.interaction;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.yyfax.common.R;

import java.util.List;

/**
 * Created by fan on 2017/4/1.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public abstract class SearchBarInteraction {
	protected int NO_TYPE = -1;
	protected Context mContext;
	protected boolean mFillSearchContent;
	protected SearchBarListener mSearchBarListener;
	protected View mShadowView;


	public SearchBarInteraction(Context context, TypedArray ta, View view) {
		mFillSearchContent = ta.getBoolean(R.styleable.SearchBar_fillSearchContent, true);
		mContext = context.getApplicationContext();
		String mHind = ta.getString(R.styleable.SearchBar_android_hint);
		if (!TextUtils.isEmpty(mHind)) {
			((TextView) view.findViewById(R.id.search_content_et)).setHint(mHind);
		}
		initView(context, ta, view);
	}

	protected abstract void initView(Context context, TypedArray ta, View view);

	public interface SearchBarListener {
		void searchContent(int searchType, String content);
	}

	public void setSearchContent(int SearchType, String content) {
	}

	public void setSearchContent(String content) {
	}

	public void setSearchBarListener(SearchBarListener listener) {
		mSearchBarListener = listener;
	}

	public void setShadow(View shadowView) {
		if (shadowView != null) {
			mShadowView = shadowView;
			setListenerToShadowView();
		}
	}
	public void setSearchTypes(List<String> Types) {}

	public void setOverdueType(int type){}
	/**
	 * @param hasShadow
	 */
	protected void resetShadowState(boolean hasShadow) {
		if (mShadowView != null) {
			mShadowView.setVisibility(hasShadow ? View.VISIBLE : View.GONE);
		}
	}

	protected void setListenerToShadowView() {
	}

	/**
	 * 设置 输入框是否自动填充搜索时的数据
	 */
	public void setFillSearchContent(boolean fillSearchContent) {
		mFillSearchContent = fillSearchContent;
	}

	/**
	 * *隐藏输入法(Activity)
	 */
	protected void hideInputWindow(View view) {
		((InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
		view.clearFocus();//清除当前view的焦点
		view.setFocusable(false);
	}

	/**
	 * *显示输入法(Activity)
	 */
	protected void showInputWindow(View view) {
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.requestFocus();
		view.requestFocusFromTouch();
		InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		inputManager.showSoftInput(view, 0);
	}


}
